﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace Rabbit.API.Controllers
{
    /// <summary>
    /// 图片上传控制器 
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class UploadsController : ControllerBase
    {
        
        IWebHostEnvironment env;

        public UploadsController(IWebHostEnvironment env)
        {
            this.env = env;
        }

        /// <summary>
        /// 图片上传
        /// </summary>
        /// <param name="file">路径</param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Loadway(IFormFile file)
        {
            try
            {
                string path = env.WebRootPath + "/ImgFiles/" + file.FileName; //存放图片地址
                using (FileStream fs = System.IO.File.Create(path))  //文件流
                {
                    file.CopyTo(fs);
                    fs.Flush();
                }
                return Ok("http://localhost:5048/ImgFiles/" + file.FileName);
            }
            catch (Exception)
            {
                
                throw;
            }


        }
    }
}
